package com.oneday.nuo.base.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.StrUtil;
import com.anji.captcha.model.vo.CaptchaVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oneday.nuo.base.common.NuoUtil;
import com.oneday.nuo.base.config.BaseService;
import com.oneday.nuo.base.dao.NuoAuthDAO;
import com.oneday.nuo.base.dto.NuoAuthDTO;
import com.oneday.nuo.base.dto.NuoAuthorizationDTO;
import com.oneday.nuo.base.entity.NuoAuthEntity;
import com.oneday.nuo.base.entity.NuoMenuEntity;
import com.oneday.nuo.base.security.user.NuoAuth;
import com.oneday.nuo.base.service.NuoAuthService;
import com.oneday.nuo.base.service.NuoMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.CollectionUtils;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * 权限表(NuoAuth)表服务实现类
 *
 * @author liushuai
 * @since 2021-03-13 23:36:54
 */
@Service("nuoAuthService")
public class NuoAuthServiceImpl extends BaseService<NuoAuthDAO, NuoAuthEntity> implements NuoAuthService {

    @Autowired
    private NuoAuthDAO nuoAuthDao;
    @Autowired
    private NuoMenuService nuoMenuService;


    @Override
    public void saveAuth(NuoAuthDTO nuoAuthDTO) {
        if(ObjectUtils.isEmpty(nuoAuthDTO.getSortIndex())) {
            nuoAuthDTO.setSortIndex(NuoUtil.DEFAULT_SORT);
        }
        NuoAuthEntity entity = Convert.convert(NuoAuthEntity.class, nuoAuthDTO);
        this.saveOrUpdate(entity);

    }

    @Override
    public NuoAuthEntity queryByAuthId(String authId) {
        return nuoAuthDao.selectById(authId);
    }

    @Override
    public void deleteByAuthId(String authId) {
        nuoAuthDao.deleteById(authId);

    }

    @Override
    public IPage<NuoAuthEntity> queryAuthPageByParam(Page page, NuoAuthDTO nuoAuthDTO) {
        LambdaQueryWrapper<NuoAuthEntity> wrapper = new LambdaQueryWrapper();
        wrapper.like(StrUtil.isNotEmpty(nuoAuthDTO.getAuthName()),NuoAuthEntity::getAuthName,nuoAuthDTO.getAuthName());
        wrapper.like(StrUtil.isNotEmpty(nuoAuthDTO.getMenuName()),NuoAuthEntity::getMenuName,nuoAuthDTO.getMenuName());
        wrapper.eq(StrUtil.isNotEmpty(nuoAuthDTO.getMenuId()),NuoAuthEntity::getMenuId,nuoAuthDTO.getMenuId());
        wrapper.orderByDesc(NuoAuthEntity::getCreateDate);
        return nuoAuthDao.selectPage(page, wrapper);
    }

    @Override
    public List<Tree<String>> queryAuthAll() {
        List<NuoMenuEntity> nuoMenuEntities = nuoMenuService.queryMenuAllList();
        List<NuoAuthorizationDTO> lists = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(nuoMenuEntities)){
            nuoMenuEntities.forEach(res->{
                lists.add(NuoAuthorizationDTO.builder()
                        .id(res.getId())
                        .name(res.getName())
                        .pid(res.getPid())
                        .sortIndex(res.getSortIndex())
                        .build());
            });
        }
        LambdaQueryWrapper<NuoAuthEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(NuoAuthEntity::getAuthId,NuoAuthEntity::getAuthName,NuoAuthEntity::getMenuId);
        List<NuoAuthEntity> nuoAuthEntities = nuoAuthDao.selectList(wrapper);
        if(CollectionUtil.isNotEmpty(nuoAuthEntities)){
            nuoAuthEntities.forEach(res->{
                lists.add(NuoAuthorizationDTO.builder()
                        .id(res.getAuthId())
                        .pid(res.getMenuId())
                        .name(res.getAuthName())
                        .sortIndex(res.getSortIndex())
                        .build());
            });
        }
        List<Tree<String>> treeNodes = TreeUtil.build(lists, "0", new TreeNodeConfig() ,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId());
                    tree.setParentId(treeNode.getPid());
                    tree.setWeight(treeNode.getSortIndex());
                    tree.setName(treeNode.getName());
                });



        return treeNodes;
    }

    @Override
    public List<NuoAuth> queryAuthByUserId(Long userId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("c.user_id",userId);
        List<NuoAuth> auths = nuoAuthDao.queryAuthByUserId(wrapper);
        return auths;
    }
}
